package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.basic.ErpProductMapper;
import com.canaan.business.domain.basic.ErpProduct;
import com.canaan.business.service.basic.IErpProductService;
import com.canaan.common.annotation.TableName;
/**
 * ERP物料主数据Service业务层处理
 *
 * @author canaan
 * @date 2025-03-23
 */
@Service
public class ErpProductServiceImpl implements IErpProductService {
    @Resource
    private ErpProductMapper erpProductMapper;

    /**
     * 查询ERP物料主数据
     *
     * @param id ERP物料主数据主键
     * @return ERP物料主数据
     */
    @Override
    public ErpProduct selectErpProductById(Long id) {
        return erpProductMapper.selectErpProductById(id);
    }

    /**
     * 查询ERP物料主数据列表
     *
     * @param erpProduct ERP物料主数据
     * @return ERP物料主数据
     */
    @Override
    public List<ErpProduct> selectErpProductList(ErpProduct erpProduct) {
        return erpProductMapper.selectErpProductList(erpProduct);
    }

    /**
     * 新增ERP物料主数据
     *
     * @param erpProduct ERP物料主数据
     * @return 结果
     */
    @Override
    public int insertErpProduct(ErpProduct erpProduct) {
        if(StringUtils.isBlank(erpProduct.getNo())){
            erpProduct.setNo(TableNoUtil.getNo(ErpProduct.class));
        }
        if(StringUtils.isEmpty(erpProduct.getCreateBy())) {
            erpProduct.setCreateBy(SecurityUtils.getUsername());
        }
        erpProduct.setCreateTime(DateUtils.getNowDate());
        return erpProductMapper.insertErpProduct(erpProduct);
    }

    /**
     * 批量新增ERP物料主数据
     *
     * @param erpProductList ERP物料主数据列表
     * @return 结果
     */
    @Override
    public int batchInsertErpProduct(List<ErpProduct> erpProductList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ErpProduct erpProduct : erpProductList){
            if(StringUtils.isBlank(erpProduct.getNo())){
                erpProduct.setNo(TableNoUtil.getNo(ErpProduct.class));
            }
            erpProduct.setCreateBy(username);
            erpProduct.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<erpProductList.size();) {
            int endIndex = i+500;
            if (endIndex > erpProductList.size()) {
                endIndex = erpProductList.size();
            }
            insertNum = insertNum + erpProductMapper.batchInsertErpProduct(erpProductList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改ERP物料主数据
     *
     * @param erpProduct ERP物料主数据
     * @return 结果
     */
    @Override
    public int updateErpProduct(ErpProduct erpProduct) {
        if(StringUtils.isEmpty(erpProduct.getUpdateBy())) {
            erpProduct.setUpdateBy(SecurityUtils.getUsername());
        }
        erpProduct.setUpdateTime(DateUtils.getNowDate());
        return erpProductMapper.updateErpProduct(erpProduct);
    }

    /**
     * 批量修改ERP物料主数据
     *
     * @param erpProductList ERP物料主数据列表
     * @return 结果
     */
    @Override
    public int batchUpdateErpProduct(List<ErpProduct> erpProductList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ErpProduct erpProduct : erpProductList){
            erpProduct.setUpdateBy(username);
            erpProduct.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<erpProductList.size();) {
            int endIndex = i+500;
            if (endIndex > erpProductList.size()) {
                endIndex = erpProductList.size();
            }
            updateNum = updateNum + erpProductMapper.batchUpdateErpProduct(erpProductList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除ERP物料主数据
     *
     * @param ids 需要删除的ERP物料主数据主键集合
     * @return 结果
     */
    @Override
    public int deleteErpProductByIds(Long[] ids) {
        return erpProductMapper.deleteErpProductByIds(ids);
    }

    /**
     * 删除ERP物料主数据信息
     *
     * @param id ERP物料主数据主键
     * @return 结果
     */
    @Override
    public int deleteErpProductById(Long id) {
        return erpProductMapper.deleteErpProductById(id);
    }

    /**
     * 查询ERP物料主数据
     *
     * @param no 编号
     * @return ERP物料主数据
     */
    @Override
    public ErpProduct selectErpProductByNo(String no) {
        return erpProductMapper.selectErpProductByNo(no);
    }
    /**
     * 查询ERP物料主数据
     *
     * @param productNo 编号
     * @return ERP物料主数据
     */
    @Override
    public ErpProduct selectErpProductByProductNo(String productNo) {
        ErpProduct product = erpProductMapper.selectErpProductByProductNo(productNo);
        if (ObjectUtil.isEmpty(product)) {
            product = erpProductMapper.selectErpProductByBarCode(productNo);
        }
        return product;
    }

    @Override
    public ErpProduct selectErpProductByBarCode(String productNo) {
        return erpProductMapper.selectErpProductByBarCode(productNo);
    }

    /**
     * 批量删除ERP物料主数据
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteErpProductByNos(String[] nos) {
        return erpProductMapper.deleteErpProductByNos(nos);
    }

    /**
     * 删除ERP物料主数据信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteErpProductByNo(String no) {
        return erpProductMapper.deleteErpProductByNo(no);
    }

    @Override
    public List<ErpProduct> listByStorehouseNo(ErpProduct erpProduct) {
        List<ErpProduct> list = erpProductMapper.listByStorehouseNo(erpProduct);
        if (CollUtil.isEmpty(list) && StringUtils.isNotEmpty(erpProduct.getProductNo())) {
            //用69码查询数据
            erpProduct.setBarCode(erpProduct.getProductNo());
            erpProduct.setProductNo(null);
            list = erpProductMapper.listByStorehouseNo(erpProduct);
        }
        return list;
    }
}
